package de.karlNet.sambamanager.view.beans.forms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Controller;

import de.karlNet.sambamanager.ldapController.commands.CreateOrganizationalUnit;
import de.karlNet.sambamanager.ldapModel.LdapContainer;
import de.karlNet.sambamanager.view.beans.ManualLeafHandlingTreeNode;
import de.karlNet.sambamanager.view.beans.trees.SelectedNodeContainer;

@Controller
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class CreateOUBean extends ViewBean {
	@Autowired
	private CreateOrganizationalUnit createOrganizationalUnit;

	protected LdapContainer container = new LdapContainer();

	@Autowired
	protected SelectedNodeContainer nodeContainer;

	public LdapContainer getContainer() {
		return container;
	}

	public void setContainer(LdapContainer container) {
		this.container = container;
	}

	public String create() {
		try {
			this.container.setDn("ou=" + this.container.getCn() + ","
					+ this.nodeContainer.selectedNode.getLdapObject().getDn());
			this.createOrganizationalUnit
					.createOrganisationalUnit(this.container);
			if (this.nodeContainer.selectedNode.isExpanded()) {
				new ManualLeafHandlingTreeNode("container", this.container,
						this.nodeContainer.selectedNode);
			}
			this.container = new LdapContainer();
			this.setShow(false);
			return "index.xhtml";
		} catch (Exception e) {
			this.showErrorMessage(e);
			return "";
		}
	}

	@Override
	public String getPageName() {
		return "createOUView";
	}
}
